package com.woniu.user.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.common.ResponseData;
import com.woniu.user.entity.Face;
import com.woniu.user.service.FaceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/face")
public class FaceController {

    @Autowired
    private FaceService faceService;

    @GetMapping("/faceList")
    @ApiOperation(value = "人脸列表", notes = "查询所有的人脸信息")
    public ResponseData<?> faceList(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String faceName,
            @RequestParam(required = false) String faceStatus
    ) {
        IPage<Face> page = faceService.page(new Page(current, size), new QueryWrapper<Face>()
                .like(StrUtil.isNotBlank(faceName), "face_name", faceName)
                .like(StrUtil.isNotBlank(faceStatus), "face_status", faceStatus));
        //return FaceResult.success(page);
        return new ResponseData<>().ok(page);
    }


    @GetMapping("/faceDelete/{fid}")
    @ApiOperation(value = "删除人脸", notes = "根据id进行删除")
    public ResponseData<?> faceDelete(@PathVariable Integer fid) {
        faceService.removeById(fid);
        return new ResponseData<>().ok("删除成功");
    }

    @GetMapping("/info/{fid}")
    @ApiOperation(value = "查询方法", notes = "根据id查询人脸信息")
    public ResponseData<?> info(@PathVariable Integer fid) {
        return new ResponseData<>().ok(faceService.getById(fid));
    }

    @PostMapping("/saveFace")
    @ApiOperation(value = "添加")
    public ResponseData<?> save(@RequestBody Face face) {
        face.setVefNum(0);
        face.setCreateTime(new Date());
        faceService.save(face);
        return new ResponseData<>().ok("添加成功");
    }

    @PostMapping("/updateFace")
    @ApiOperation(value = "修改")
    public ResponseData<?> update(@RequestBody Face face) {
        faceService.updateById(face);
        return new ResponseData<>().ok("修改成功");
    }

    @GetMapping("/faceListAndUserId")
    @ApiOperation(value = "人脸列表", notes = "查询所有的人脸信息")
    public ResponseData<?> faceListAndUserId(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) String faceName,
            @RequestParam(required = false) String faceStatus,
            @RequestParam Integer userId // 接收用户ID
    ) {
        QueryWrapper<Face> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fid", userId); // 根据用户ID查询
        if (StrUtil.isNotBlank(faceName)) {
            queryWrapper.like("face_name", faceName);
        }
        if (StrUtil.isNotBlank(faceStatus)) {
            queryWrapper.eq("face_status", faceStatus);
        }
        IPage<Face> page = faceService.page(new Page<>(current, size), queryWrapper);
        return new ResponseData<>().ok(page);
    }
}
